package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.AssistEntType;
import com.cz.nongyetongv5.bean.AssistEntTypeBean;
import com.cz.nongyetongv5.bean.JsonUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class AssistEntListTypeActivity extends Activity implements View.OnClickListener {

    private RelativeLayout btnBack;
    RecyclerView recyclerView;
    private String region_code;
    private String year;
    private int type;
    private TextView tv_title;
    private String lacationTag;
    private List<AssistEntType> assistEntTypes;
    private ListAdapter adapter;
    private int isSame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assist_ent_list_type);
        btnBack = findViewById(R.id.btnBack);
        tv_title = findViewById(R.id.tv_title);
        btnBack.setOnClickListener(this);
        recyclerView = findViewById(R.id.recyclerView);
        assistEntTypes = new ArrayList<>();
        region_code = getIntent().getStringExtra("region_code");
        year = getIntent().getStringExtra("year");
        type = getIntent().getIntExtra("type", 0);
        isSame = getIntent().getIntExtra("isSame", 0);
        if (type == 0) {
            tv_title.setText("全部主体分类汇总");
            getList();
        } else if (type == 1) {
            tv_title.setText("新增主体分类汇总");
            lacationTag = "1";
            getListType();
        } else if (type == 2) {
            tv_title.setText("更新主体分类汇总");
            lacationTag = "2,4";
            getListType();
        } else if (type == 3) {
            tv_title.setText("注销主体分类汇总");
            lacationTag = "3";
            getListType();
        }
        adapter = new ListAdapter();
        View notDataView = getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) recyclerView.getParent(), false);
        adapter.setEmptyView(notDataView);
        recyclerView.setLayoutManager(new LinearLayoutManager(AssistEntListTypeActivity.this));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(AssistEntListTypeActivity.this, AssistEntListActivity.class);
                intent.putExtra("region_code", region_code);
                intent.putExtra("year", year);
                intent.putExtra("type", type);
                intent.putExtra("isSame",isSame);
                intent.putExtra("kind", assistEntTypes.get(position).getKind());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnBack:
                finish();
                break;
            default:
                break;
        }
    }


    void getList() {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "ent/allKindEnt.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("region_code", region_code)
                .addParams("isSame", String.valueOf(isSame))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(AssistEntListTypeActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.println(response);
                        AssistEntTypeBean assistEntTypeBean = JsonUtil.toObject(response, AssistEntTypeBean.class);
                        if (assistEntTypeBean != null) {
                            if (assistEntTypeBean.getCode() == 0) {
                                assistEntTypes.clear();
                                assistEntTypes.addAll(assistEntTypeBean.getData());
                                adapter.notifyDataSetChanged();
                            } else {
                                Toast.makeText(AssistEntListTypeActivity.this, "获取数据失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
    }


    void getListType() {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "ent/allKindAssistEnt.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("region_code", region_code)
                .addParams("year", year)
                .addParams("lacationTag", lacationTag)
                .addParams("isSame", String.valueOf(isSame))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(AssistEntListTypeActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.println(response);
                        AssistEntTypeBean assistEntTypeBean = JsonUtil.toObject(response, AssistEntTypeBean.class);
                        if (assistEntTypeBean != null) {
                            if (assistEntTypeBean.getCode() == 0) {
                                assistEntTypes.clear();
                                assistEntTypes.addAll(assistEntTypeBean.getData());
                                adapter.notifyDataSetChanged();
                            } else {
                                Toast.makeText(AssistEntListTypeActivity.this, "获取数据失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
    }


    public class ListAdapter extends BaseQuickAdapter<AssistEntType, BaseViewHolder> {
        public ListAdapter() {
            super(R.layout.assist_enttype_item, assistEntTypes);
        }

        @Override
        protected void convert(BaseViewHolder helper, AssistEntType item) {
            helper.setText(R.id.tv_name, item.getKind_name() + "【" + item.getKindCount() + "户】");
        }
    }
}
